SyntaxError: Der Property-Name __proto__ erscheint mehr als einmal im Objektliteral

Der JavaScript-Fehler "property name __proto__ appears more than once in object literal" tritt auf, wenn ein Objektliteral mehrfach das __proto__-Feld enthält, das verwendet wird, um das Prototyp des neuen Objekts festzulegen.

Meldung

SyntaxError: Duplicate __proto__ fields are not allowed in object literals (V8-based)
SyntaxError: property name __proto__ appears more than once in object literal (Firefox)
SyntaxError: Attempted to redefine __proto__ property. (Safari)

Fehlertyp

Was ist schiefgelaufen?

Der Schlüssel __proto__, im Gegensatz zu anderen Eigenschafts-Schlüsseln, ist eine spezielle Syntax in einem Objektliteral. Er wird verwendet, um den Prototyp des erstellten Objekts festzulegen und darf nicht mehr als einmal in einem Objektliteral erscheinen. Beachten Sie, dass diese Einschränkung nur auf die __proto__ Prototyp-Setter-Syntax zutrifft: wenn es tatsächlich die Wirkung hat, eine Eigenschaft namens __proto__ zu erstellen, dann kann es mehrmals erscheinen. Siehe Prototyp-Setter für die genauen Syntaxbeschränkungen.

Es ist erwähnenswert, dass der __proto__ Schlüssel in Objektliteralen eine spezielle Syntax ist und nicht veraltet ist, im Gegensatz zur Object.prototype.__proto__ Accessor-Eigenschaft.

Beispiele

Ungültige Fälle

js
const obj = { __proto__: {}, __proto__: { a: 1 } };

Gültige Fälle

js
// Only setting the prototype once
const obj = { __proto__: { a: 1 } };

// These syntaxes all create a property called "__proto__" and can coexist
// They would overwrite each other and the last one is actually used
const __proto__ = null;
const obj2 = {
  ["__proto__"]: {},
  __proto__,
  __proto__() {},
  get __proto__() {
    return 1;
  },
};

Siehe auch